﻿// This code is released under the BSD license.
namespace PugLib.UI.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using Extensions;
    using Filters;

    public partial class FilterPicker : UserControl
    {
        private readonly List<SearchOperator> _searchOperators;

        public FilterPicker()
        {
            InitializeComponent();

            _searchOperators = new List<SearchOperator>();
            Array values = Enum.GetValues(typeof(SearchOperator));

            foreach (SearchOperator op in values)
            {
                _searchOperators.Add(op);
            }

            FillOperators();
        }

        public SearchOperator SearchOperator
        {
            get { return _searchOperators[expressions.SelectedIndex]; }
        }

        public string Field
        {
            get { return SafeConvert.ToString(fields.SelectedItem); }
        }

        public string SearchText
        {
            get { return filter.Text; }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.delete.Click -= this.delete_Click;
                this.Load -= this.FilterPicker_Load;
            }
            base.Dispose(disposing);
        }


        private void delete_Click(object sender, EventArgs e)
        {
            // tell parent FilterPickerGroup to remove this Filter and delete from Filter collection
            if (Parent == null)
            {
                throw new InvalidOperationException("FilterPicker must have a grandparent of type FilterPickerGroup");
            }

            FilterPickerGroup parent = Parent.Parent as FilterPickerGroup;
            if (parent == null)
            {
                throw new InvalidOperationException("FilterPicker must have a grandparent of type FilterPickerGroup");
            }

            parent.RemoveFilterPicker(this);
        }


        /// <summary>
        /// Adds the provided collection in A-Z sorted order.
        /// </summary>
        /// <param name="items"></param>
        public void SetFields<T>(IEnumerable<T> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            IOrderedEnumerable<T> list = from item in items
                                         orderby item ascending
                                         select item;

            foreach (T member in list)
            {
                fields.Items.Add(member);
            }

            fields.SelectedIndex = 0;
        }

        private void FillOperators()
        {
            foreach (SearchOperator member in _searchOperators)
            {
                expressions.Items.Add(member.GetDescription());
            }

            expressions.SelectedIndex = 0;
        }

        /// <summary>
        /// Selects the provided expression in the list
        /// </summary>
        /// <param name="expr"></param>
        public void SetSearchOperator(SearchOperator expr)
        {
            expressions.SelectedIndex = expressions.Items.IndexOf(expr.GetDescription());
        }

        /// <summary>
        /// Selects the provided field in the list
        /// </summary>
        /// <param name="field"></param>
        public void SetField(string field)
        {
            fields.SelectedIndex = fields.Items.IndexOf(field);
        }

        /// <summary>
        /// Sets the freetext expression
        /// </summary>
        /// <param name="searchText"></param>
        public void SetFilter(string searchText)
        {
            filter.Text = searchText;
        }


        private void FilterPicker_Load(object sender, EventArgs e)
        {
            Dock = DockStyle.Top;
        }
    }
}